package com.zxs.product.controller;

import com.zxs.product.service.ProductService;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import parm.*;
import pojo.Product;
import utils.R;

import java.util.Map;

/**
 * @author zxs
 * *7 18:14
 */
@RestController
@RequestMapping("product")
public class ProductController {

    @Autowired
    private ProductService productService;


    @PostMapping("search")
    public R productList(@RequestBody ProductParamsSearch productParamsSearch) throws JsonProcessingException {
        return productService.search(productParamsSearch);
    }



    @GetMapping("/hello")
    public String hello() {
        return "Hello";
    }


    @PostMapping("/promo")
    public R promo(@RequestBody @Validated ProductPromoParam productPromoParam, BindingResult result) {

        if (result.hasErrors()) {
            return R.fail("数据查询失败!");
        }

        return productService.promo(productPromoParam.getCategoryName());
    }

    @PostMapping("hots")
    public R hots(@RequestBody @Validated ProductHotParam productHotParam, BindingResult result) {
        if (result.hasErrors()) {
            return R.fail("数据查询失败！???");
        }
        return productService.hots(productHotParam);
    }

    @PostMapping("category/list")
    public R list() {
        return productService.clist();
    }

    @PostMapping("bycategory")
    public R bycategory(@RequestBody @Validated CategoryParamInteger categoryParamInteger, BindingResult result) {
        if (result.hasErrors()) {
            return R.fail("类别商品查询失败");
        }
        return productService.bycategory(categoryParamInteger);
    }

    @PostMapping("all")
    public R all(@RequestBody @Validated CategoryParamInteger categoryParamInteger, BindingResult result) {
        if (result.hasErrors()) {
            return R.fail("全部商品查询失败！");
        }
        return productService.bycategory(categoryParamInteger);
    }

    @PostMapping("detail")
    public R detail(@RequestBody Map<String, Integer> param) {
        Integer productID = param.get("productID");
        return productService.detail(productID);
    }

    @GetMapping("list")
    public R list(@RequestBody @Validated PageParam pageParam, BindingResult result) {
        if (result.hasErrors()) {
            return R.fail("全部商品查询失败！");
        }
        return productService.getProducts(pageParam);
    }

    @PostMapping("save")
    public R adminSave(@RequestBody ProductSaveParam productSaveParam) {
        return productService.adminSave(productSaveParam);
    }

    @PostMapping("remove")
    R remove(@RequestBody Integer productId) {
        return productService.adminDelete(productId);
    }

    @PostMapping("update")
    R update(@RequestBody Product product) {
        return productService.update(product);
    }

}
